import {
  fetchPlaymentTypeList,
  getIconBalance,
  unitedPaV1
} from "../../api/modules/payment";
import {
  fetchOrderIconPreview, orderCoin
} from "../../api/modules/trade";
import {
  loginController
} from "../../utils/login";
import { accAdd } from "../../utils/number.utils";

const duration = 5 * 1000 * 60;


const getDescLabel = function (name) {
  return `${name}服务为虚拟商品、可用作剧集解锁`
}
// /Users/wanghongsheng/工作/miniprograms/ShortPlayerMiniApp/pages-mine/recharge/recharge.js
Page({
  data: {
    loading: false,
    loading2: false,
    loading3: false,
    title: "我的",
    isNeedRefresh: false,
    balance: null,
    selectedSkuId:-1,
    previewCode:null,
    selectedId: -1,
    payTypeList: [],
    skuList: [],
    desc: ""
  },
  timer: null,
  onLoad: async function (options) {
    this.fetchBalance();
    this.fetchPaymentTypeList();
    this.fetchCoinPreview();
  },
  async fetchBalance() {
    await loginController.login()
    this.setData({
      loading: true
    })

    try {
      const {
        data
      } = await getIconBalance(null, {
        loading: false
      });
      this.setData({
        title: `我的${data.virtualCoinName}`,
        desc: getDescLabel(data.virtualCoinName),
        balance: data,
        loading: false
      })
    } catch (error) {
      tt.showToast({
        title: "获取服务失败"
      });
    }
  },
  async fetchCoinPreview() {
    this.setData({loading3:true})
    const {
      data
    } = await fetchOrderIconPreview();
    const {
      previewCode,
      virtualCoin,
      virtualCoinPromotion
    } = data;
    let skuList = virtualCoinPromotion.skuList.map(v => {
      return {
        previewCode,
        skuId: v.id,
        coinNum:accAdd(v.rechargeAmount, v.presentAmount),
        coinName:virtualCoin.name,
        coinPrice:v.price,
      }
    })

    let target=skuList.find(v=>v.skuId===this.data.selectedSkuId)
    this.timer = clearTimeout(this.timer);
    this.timer = setTimeout(() => {
      this.setData({
        isNeedRefresh: true
      })
    }, duration);
    this.setData({
      skuList,
      loading3:false,
      isNeedRefresh: false,
      selectedSkuId:target?target.id:skuList[0].skuId
    })
  },
  async fetchPaymentTypeList() {
    this.setData({loading2:true})
    const {
      data
    } = await fetchPlaymentTypeList();
    let payTypeList = data.showList;
    let leastPayTypeId = tt.getStorageSync("payTypeId");
    let target = payTypeList.find(v => v.id == leastPayTypeId);
    let target2 = payTypeList.find(v => v.selectedDefault);
    let selectedId = -1;
    if (target) {
      selectedId = target.id;
    } else if (target2) {
      selectedId = target2.id;
    } else if (payTypeList.length > 1) {
      selectedId = payTypeList[0].id;
    }
    this.setData({
      selectedId,
      payTypeList,
      loading2:false
    })
  },
  togglePaytype(e) {
    let id = e.currentTarget.dataset.id;
    if (id == this.data.selectedId) return;
    this.setData({
      selectedId: id
    })
    tt.setStorageSync("payTypeId", id);
  },
  toggleSkuId(e){
    let skuId=e.currentTarget.dataset.skuId;
    if(skuId===this.data.selectedSkuId)return;
    this.setData({
      selectedSkuId:skuId
    })
  },
  showRechangeTerm(){},
  //
  async handleSubmit() {
    let {skuList,selectedSkuId,selectedId,payTypeList}=this.data;
    let _target1=skuList.find(v=>v.skuId===selectedSkuId);
    let _target2=payTypeList.find(v=>v.id===selectedId);
    if(_target2.paymentMode==='模拟支付'){
      const {data}=await orderCoin({
        paymentModeId: _target2.paymentModeId,
        previewCode:_target1.previewCode,
        selectedSkuId: selectedSkuId
      },{loading:true})
      //TODO
      // orderNo, paymentModeId 
      await unitedPaV1({orderNo:data.orderNo,paymentModeId:data.paymentModeId},{loading:true})
      
      tt.showToast({
        title: "充值成功",
        success: (res) => {
          tt.navigateBack({
            delta:1,
            success: (res) => {
              
            },
            fail: (res) => {
              
            },
          });
        },
        fail: (res) => {
          
        },
      });
    }
   
  },
  async handleRetry(){
    await loginController.login()
    this.fetchPaymentTypeList();
    this.fetchCoinPreview();
  }
})